package com.xkdx.guangguang.fragment.login;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.xkdx.guangguang.fragment.user.UserSharePrefence;
import com.xkdx.guangguang.fragment.user.UserSharePrefence1;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;

public class UserModule extends AbsModule {
	
	private Context context;
	public User user;
	
	public UserModule(Context context) {
		this.context = context;
	}
	
	@Override
	public void parseData() throws Exception {
		try {
			backJson = (JSONArray) result;
			for (int i = 0; i < backJson.length(); i++) {
				jsonObj = (JSONObject) backJson.get(i);
				parseUser(jsonObj);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void parseUser(JSONObject json ) {
		try {
			user = new User();
			user.code = json.getString("Code");
			user.message = json.getString("Message");
			Detail detailInfo = parseDetail(json.getJSONObject("DetailInfo"));
			user.detailInfo = detailInfo;
			if(user.code.equals("0000")) {
				UserSharePrefence1 userSp = new UserSharePrefence1(context, IConstants.SP_USER_NEW);
				userSp.setId(detailInfo.getId());
				userSp.setUserLoginName(detailInfo.getUserLoginName());
				userSp.setUserPass(detailInfo.getUserPass());
				userSp.setUserNickName(detailInfo.getUserNickName());
				userSp.setUserPicAssetId(detailInfo.getUserPicAssetId());
				userSp.setSignWords(detailInfo.getSignWords());
				userSp.setLastLoginIp(detailInfo.getLastLoginIp());
				userSp.setLastLoginTime(detailInfo.getLastLoginTime());
				userSp.setLogintoken(detailInfo.getLogintoken());
				userSp.setScore(detailInfo.getScore());
				userSp.setMoney(detailInfo.getMoney());
//				userSp.setRedpacket(detailInfo.getRedpacket());
				userSp.setDevicetype(detailInfo.getDevicetype());
				userSp.setLevel(detailInfo.getLevel());
				userSp.setVerify(detailInfo.getVerify());
				userSp.setStatus(detailInfo.getStatus());
				userSp.setBirthday(detailInfo.getBirthday());
				userSp.setUsertype(detailInfo.getUsertype());
				userSp.setShopids(detailInfo.getShopids());
				userSp.setSex(detailInfo.getSex());
				
				UserSharePrefence userSpOld = new UserSharePrefence(context, IConstants.SP_USER);
				userSpOld.setUserID(detailInfo.getId());
				userSpOld.setUserLoginToken(detailInfo.getLogintoken());
				userSpOld.setLoginToken(detailInfo.getLogintoken());
				userSpOld.setUserSex(detailInfo.getSex());
				userSpOld.setUserBirthday(detailInfo.getBirthday());
				userSpOld.setVerifyType(detailInfo.getVerify());
				userSpOld.setUserType(detailInfo.getUsertype());
				userSpOld.setUserStatus(detailInfo.getStatus());
				userSpOld.setUserNickName(detailInfo.getUserNickName());
				userSpOld.setUserLoginName(detailInfo.getUserLoginName());
				userSpOld.setUserPassword(detailInfo.getUserPass());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private Detail parseDetail(JSONObject json) {
		if(json == null) {
			return null;
		}
		Detail detail = new Detail();
		try {
			detail.setId(json.getString("id"));
			detail.setUserLoginName(json.getString("user_login_name"));
			detail.setUserPass(json.getString("user_pass"));
			detail.setUserNickName(json.getString("user_nickname"));
			detail.setUserPicAssetId(json.getString("user_pic_assetid"));
			detail.setSignWords(json.getString("sign_words"));
			detail.setLastLoginIp(json.getString("last_login_ip"));
			detail.setLastLoginTime(json.getString("last_login_time"));
			detail.setCreateTime(json.getString("create_time"));
			detail.setLogintoken(json.getString("logintoken"));
			detail.setScore(json.getString("score"));
			detail.setMoney(json.getString("money"));
			detail.setRedpacket(json.getString("redpacket"));
			detail.setDevicetype(json.getString("devicetype"));
			detail.setLevel(json.getString("level"));
			detail.setVerify(json.getString("verify"));
			detail.setStatus(json.getString("status"));
			detail.setBirthday(json.getString("birthday"));
			detail.setUsertype(json.getString("usertype"));
			detail.setShopids(json.getString("shopids"));
			detail.setSex(json.getString("sex"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return detail;
	}
	
	public class User {
		String code;
		String message;
		Detail detailInfo;
	}
	
	public class Detail {
		private String id;
		private String userLoginName;
		private String userPass;
		private String userNickName;
		private String userPicAssetId;
		private String signWords;
		private String lastLoginIp;
		private String lastLoginTime;
		private String createTime;
		private String logintoken;
		private String score;
		private String money;
		private String redpacket;
		private String devicetype;
		private String level;
		private String verify;
		private String status;
		private String birthday;
		private String usertype;
		private String shopids;
		private String sex;
		
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getUserLoginName() {
			return userLoginName;
		}
		public void setUserLoginName(String userLoginName) {
			this.userLoginName = userLoginName;
		}
		public String getUserPass() {
			return userPass;
		}
		public void setUserPass(String userPass) {
			this.userPass = userPass;
		}
		public String getUserNickName() {
			return userNickName;
		}
		public void setUserNickName(String userNickName) {
			this.userNickName = userNickName;
		}
		public String getUserPicAssetId() {
			return userPicAssetId;
		}
		public void setUserPicAssetId(String userPicAssetId) {
			this.userPicAssetId = userPicAssetId;
		}
		public String getSignWords() {
			return signWords;
		}
		public void setSignWords(String signWords) {
			this.signWords = signWords;
		}
		public String getLastLoginIp() {
			return lastLoginIp;
		}
		public void setLastLoginIp(String lastLoginIp) {
			this.lastLoginIp = lastLoginIp;
		}
		public String getLastLoginTime() {
			return lastLoginTime;
		}
		public void setLastLoginTime(String lastLoginTime) {
			this.lastLoginTime = lastLoginTime;
		}
		public String getCreateTime() {
			return createTime;
		}
		public void setCreateTime(String createTime) {
			this.createTime = createTime;
		}
		public String getLogintoken() {
			return logintoken;
		}
		public void setLogintoken(String logintoken) {
			this.logintoken = logintoken;
		}
		public String getScore() {
			return score;
		}
		public void setScore(String score) {
			this.score = score;
		}
		public String getMoney() {
			return money;
		}
		public void setMoney(String money) {
			this.money = money;
		}
		public String getRedpacket() {
			return redpacket;
		}
		public void setRedpacket(String redpacket) {
			this.redpacket = redpacket;
		}
		public String getDevicetype() {
			return devicetype;
		}
		public void setDevicetype(String devicetype) {
			this.devicetype = devicetype;
		}
		public String getLevel() {
			return level;
		}
		public void setLevel(String level) {
			this.level = level;
		}
		public String getVerify() {
			return verify;
		}
		public void setVerify(String verify) {
			this.verify = verify;
		}
		public String getStatus() {
			return status;
		}
		public void setStatus(String status) {
			this.status = status;
		}
		public String getBirthday() {
			return birthday;
		}
		public void setBirthday(String birthday) {
			this.birthday = birthday;
		}
		public String getUsertype() {
			return usertype;
		}
		public void setUsertype(String usertype) {
			this.usertype = usertype;
		}
		public String getShopids() {
			return shopids;
		}
		public void setShopids(String shopids) {
			this.shopids = shopids;
		}
		public String getSex() {
			return sex;
		}
		public void setSex(String sex) {
			this.sex = sex;
		}
	}

}
